<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--片区数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="areaName" placeholder="请输入名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-containers">
          <el-tree :data="areaOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--小区数据-->
      <el-col :span="20" :xs="24">
        <el-card class="box-card">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="客户编码" prop="customerCode">
              <el-input v-model="queryParams.customerCode" placeholder="请输入客户编码" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="表号" prop="meterCode">
              <el-input v-model="queryParams.meterCode" placeholder="请输入客户编码" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="IMEI" prop="imei">
              <el-input v-model="queryParams.imei" placeholder="请输入IMEI" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="表计状态" prop="meterStatus">
              <el-select v-model="queryParams.meterStatus" placeholder="状态" clearable style="width: 150px">
                <el-option key="1" label="待安装" value="1"></el-option>
                <el-option key="2" label="正常" value="2"></el-option>
                <el-option key="3" label="已拆卸" value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-open" size="mini" @click="startArr" :disabled="single"
                v-hasPermi="['fileManagement:civilArchives:add']">批量远程开阀</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-turn-off" size="mini" :disabled="single" @click="stopArr"
                v-hasPermi="['fileManagement:civilArchives:edit']">批量远程关阀</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="postList" border @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" :selectable="selectable" />
            <el-table-column label="客户名称" align="center" key="customerName" prop="customerName"
              :show-overflow-tooltip="true" />
            <el-table-column label="客户编码" align="center" key="customerCode" prop="customerCode"
              :show-overflow-tooltip="true" />
            <el-table-column label="客户类型" align="center" key="customerType" prop="customerType"
              :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.customer_type" :value="scope.row.customerType" />
              </template>
            </el-table-column>
            <el-table-column label="表号" align="center" prop="meterCode" width="120"/>
            <el-table-column label="DTU编号" align="center" prop="dtuCode" />
            <!-- <el-table-column label="设备ID" align="center" prop="deviceId" /> -->
            <el-table-column label="流量计厂家" align="center" prop="flowmeterManufacturer" width="120" />
            <el-table-column label="基表底数" align="center" prop="baseNumber" />
            <el-table-column label="报警额度" align="center" prop="alarmGas" />
            <el-table-column label="基表ICCID" align="center" prop="iccid" width="120" />
            <el-table-column label="IMEI" align="center" prop="imei" />
            <el-table-column label="流量计起始量" align="center" prop="initialGas" width="150" />
            <el-table-column label="安装日期" align="center" prop="installTime" width="170" />
            <el-table-column label="气表进气方向" align="center" prop="intakeDirection" width="150">
              <template slot-scope="scope">
                <span v-if="scope.row.intakeDirection == 'L'">左进</span>
                <span v-if="scope.row.intakeDirection == 'R'">右进</span>
              </template>
            </el-table-column>
            <el-table-column label="是否带流量计" align="center" prop="isFlowmeter" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.isFlowmeter == 'N'">否</span>
                <span v-if="scope.row.isFlowmeter == 'Y'">是</span>
              </template>
            </el-table-column>
            <el-table-column label="是否带GPRS" align="center" prop="isGprs" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.isGprs == 'N'">否</span>
                <span v-if="scope.row.isGprs == 'Y'">是</span>
              </template>

            </el-table-column>
            <el-table-column label="表计状态" align="center" prop="meterStatus" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.meterStatus == 1">待安装</span>
                <span v-if="scope.row.meterStatus == 2">正常</span>
                <span v-if="scope.row.meterStatus == 3">已拆卸</span>
              </template>

            </el-table-column>
            <el-table-column label="通讯地址" align="center" prop="mailAddress" />
            <el-table-column label="通讯协议" align="center" prop="mailProtocol" />
            <el-table-column label="气表品牌" align="center" prop="meterBrand" />

            <el-table-column label="基表厂家" align="center" prop="meterManufacturer" />
            <!-- <el-table-column label="基表手机号" align="center" prop="meterPhoneNum" width="120" /> -->
            <el-table-column label="表计类型" align="center" prop="meterType" />
            <!--            <el-table-column label="工程工料费" align="center" prop="projectFee" width="120" />-->
            <!--            <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />-->

            <el-table-column label="操作" class-name="small-padding fixed-width" width="180" fixed="right" align="center">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.valveStatus" active-color="#13ce66" inactive-color="#ff4949"
                  @change="(val) => switchChange(val, scope.row)" v-if="scope.row.activeStatus == 'Y'">
                </el-switch>
                <el-button size="mini" type="text" v-hasPermi="['system:instruct:openValve']"
                  v-if="scope.row.activeStatus == 'Y'">开关阀</el-button>
                <!-- <el-button size="mini" type="text" icon="el-icon-open" @click="start(scope.row)"
                  v-hasPermi="['systemSetup:gasMeter:edit']">开阀</el-button>
                <el-button size="mini" type="text" icon="el-icon-turn-off" @click="stop(scope.row)"
                  v-hasPermi="['systemSetup:gasMeter:remove']">关阀</el-button> -->
                <el-button size="mini" type="text" icon="el-icon-switch-button" @click="action(scope.row)"
                  v-hasPermi="['system:instruct:activation']" v-if="scope.row.activeStatus == 'N'">激活</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

      </el-col>
    </el-row>

  </div>
</template>

<script>
import { areaTreeList } from "@/api/fileManagement/areainfo.js"
import { openValue, closeValue, activationValue } from "@/api/service/online.js"
import { meterarchivesList } from "@/api/fileManagement/meterarchives.js"

export default {
  name: "civilArchives",
  components: {},
  dicts: ["sys_normal_disable", "customer_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区表格数据
      userList: null,
      // 弹出层标题
      title: "",
      //表计弹出层标题
      title2: "",
      // 企业树选项
      areaOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 片区名称
      areaName: undefined,
      //是否表计显示弹出层
      bjOPen: false,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        customerCode: null,
        imei: null,
        openStatus: null,
        supportNet: 1,
        isBlack: 'N'
      },
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "小区名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "小区名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        nickName: [
          { required: true, message: "小区昵称不能为空", trigger: "blur" },
        ],
      },
      activeNames: ["1"],
      postList: [],
    };
  },
  watch: {
    // 根据名称筛选企业树
    areaName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    // 多选禁用
    selectable(row, index) {
      if (row.activeStatus == 'N') {
        return false         //禁用
      } else {
        return true          //可选
      }
    },
    // 开关阀
    switchChange(event, info) {
      console.log(event, info)
      if (event == true) {
        this.start(info)
      } else {
        this.stop(info)
      }
    },
    // 开阀
    start(info) {
      let param = {
        meterIds: info.id
      }
      openValue(param).then((res) => {
        this.$modal.msgSuccess("开阀成功");
      })
    },
    // 关阀
    stop(info) {
      let param = {
        meterIds: info.id
      }
      closeValue(param).then((res) => {
        this.$modal.msgSuccess("关阀成功");
      })
    },
    //激活
    action(info) {
      let param = {
        meterIds: info.id
      }
      activationValue(param).then((res) => {
        this.$modal.msgSuccess("激活成功");
      })
    },
    // 批量开阀
    startArr() {
      let param = {
        meterIds: this.ids.join(',')
      }
      openValue(param).then((res) => {
        this.$modal.msgSuccess("开阀成功");
      })
    },
    //批量关阀
    stopArr() {
      let param = {
        meterIds: this.ids.join(',')
      }
      closeValue(param).then((res) => {
        this.$modal.msgSuccess("关阀成功");
      })
    },
    /** 查询小区列表 */
    getList() {
      this.loading = true;
      let search = this.queryParams;
      search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
      search.params['meterCode'] = search.meterCode;
      meterarchivesList(this.queryParams).then((response) => {
        let data = JSON.parse(JSON.stringify(response.rows))
        data.map((res) => {
          if (res.valveStatus == 1) {
            res.valveStatus = true
          } else {
            res.valveStatus = false
          }
        })
        this.postList = data;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询企业下拉树结构 */
    getDeptTree() {
      areaTreeList().then((response) => {
        this.areaOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.areaId = data.id;
      this.handleQuery();
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.areaId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection)
      selection.map((item) => {
        this.ids.push(item.id)
      });
      this.single = selection.length != 1;
    },




  },
};
</script>
<style lang="scss">
.head-containers {
  width: 200px;
  overflow: auto;

  .el-tree {
    min-width: 100%;
    display: inline-block !important;
  }
}
</style>
